package com.process.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.process.controller.util.Code;
import com.process.controller.util.R;
import com.process.dao.pojoDao.PracticalTaskDao;
import com.process.dao.pojoDao.TeacherDao;
import com.process.dao.publish.CampusInnerPublishDao;
import com.process.dao.team.ProcessTaskDao;
import com.process.dao.team.ProcessTaskVoDao;
import com.process.dao.voDao.CampusInnerPublishVoDao;
import com.process.dao.voDao.CampusInnerSubtaskVoDao;
import com.process.dao.voDao.PracticalTaskVoDao;
import com.process.domain.publi.Password;
import com.process.domain.publi.PracticalTask;
import com.process.domain.publi.PracticalTaskVo;
import com.process.domain.publi.Teacher;
import com.process.domain.publish.*;
import com.process.domain.team.ProcessTask;
import com.process.domain.team.ProcessTaskAo;
import com.process.domain.team.ProcessTaskVo;
import com.process.service.publish.InnerSubtaskService;
import com.process.service.score.ScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @ClassName: InnerTeacher
 * @Author: 殷凡卓
 * @Date: 2023/4/26 17:40
 */
@RestController
@Slf4j
@RequestMapping("/inner")
public class InnerController {


    @Autowired
    private PracticalTaskVoDao practicalTaskVoDao;
    @Autowired
    private CampusInnerPublishDao campusInnerPublishDao;
    @Autowired
    private InnerSubtaskService innerSubtaskService;
    @Autowired
    private CampusInnerPublishVoDao campusInnerPublishVoDao;
    @Autowired
    private CampusInnerSubtaskVoDao campusInnerSubtaskVoDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private ProcessTaskDao processTaskDao;
    @Autowired
    private ProcessTaskVoDao processTaskVoDao;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private PracticalTaskDao practicalTaskDao;

    @GetMapping("practicalTaskInnerVos")
    public R getPracticalTaskInnerVos(ServletRequest servletRequest) {

        String username = (String) servletRequest.getAttribute("username");
        Integer identity = (Integer) servletRequest.getAttribute("identity");
        List<PracticalTaskVo> practicalTaskVos = practicalTaskVoDao.selectVoByInnerAdviserPhone(username);
        return new R(Code.GET_OK, practicalTaskVos, "查询InnerVos");
    }

    @GetMapping("innerClass")
    public R getInnerClass(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        List<PracticalTask> innerClass = practicalTaskVoDao.selectClassByInnerAdviserPhone(username);

        return new R(Code.GET_OK, innerClass, "查询inner_Class");
    }

    @PostMapping("innerPublish")
    public R setInnerPublish(@RequestBody CampusInnerPublishAo campusInnerPublishAo, ServletRequest servletRequest) {
        CampusInnerPublish campusInnerPublish = campusInnerPublishAo.getCampusInnerPublish();
        List<CampusInnerSubtask> innerSubtasks = campusInnerPublishAo.getInnerSubtasks();


        if (campusInnerPublish.getStartDate() == null || campusInnerPublish.getEndDate() == null)
            return new R(Code.SAVE_ERR, null, "时间不允许为空");
        if (campusInnerPublish.getPracticalId() == 0)
            return new R(Code.SAVE_ERR, null, "请选择班级");


        int insert = campusInnerPublishDao.insert(campusInnerPublish);
        long publishId = campusInnerPublish.getPublishId();
//      遍历——给子任务表设置外键
        for (CampusInnerSubtask innerSubtask : innerSubtasks) {
            innerSubtask.setPublishId(publishId);
            System.out.println(innerSubtask);
        }
        boolean saveBatch = innerSubtaskService.saveBatch(innerSubtasks);


        return new R(Code.SAVE_OK, null, "校内实践发布成功");
    }

    @GetMapping("MyInnerPublish")
    public R getInnerPublishByTeacherPhone(ServletRequest servletRequest) {
//        获取我的电话号码
        String username = (String) servletRequest.getAttribute("username");

        List<CampusInnerPublishVo> campusInnerPublishVos = campusInnerPublishVoDao.selectByTeacherPhone(username);

        return new R(Code.GET_OK, campusInnerPublishVos, "select_innerPublishVo_byteacherPhone");
    }

    @GetMapping("Innersubtask/{publishId}")
    public R getInnersubtask(@PathVariable long publishId, ServletRequest servletRequest) {
        List<CampusInnerSubtaskVo> campusInnerSubtaskVos = campusInnerSubtaskVoDao.selectByPublishId(publishId);

        return new R(Code.GET_OK, campusInnerSubtaskVos, "select_Inner_subtask");
    }

    @GetMapping("myDetil")
    public R getmyDetil(ServletRequest servletRequest) {
//        获取我的电话号码
        String username = (String) servletRequest.getAttribute("username");
        Teacher teacher = teacherDao.selectOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeacherPhone, username));
        return new R(Code.GET_OK, teacher, "老师查看自己的详细信息");
    }

    /**
     * 校内校外共用
     * 发布过程任务
     *
     * @param processTask
     * @param servletRequest
     * @return
     */
    @PostMapping("/processTask")
    public R getprocessTask(@RequestBody ProcessTask processTask, ServletRequest servletRequest) {
//      1.数据空判断
        Date scoreStart = processTask.getScoreStart();
        Date scoreEnd = processTask.getScoreEnd();
        Date endDate = processTask.getEndDate();
        Date startDate = processTask.getStartDate();
        if (scoreEnd == null || scoreStart == null || endDate == null || startDate == null || processTask.getProcessName()==null)
            return new R(Code.SAVE_ERR, null, "不允许有空数据");
        if (scoreEnd.before(scoreStart) || endDate.before(startDate))
            return new R(Code.SAVE_ERR, null, "发布失败，请填写正确的时间");
        if (processTask.getPracticalId() == 0)
            return new R(Code.SAVE_ERR, null, "请选择班级");

//      2.查询本practicalTask最终任务selectone
        long practicalId = processTask.getPracticalId();
        ProcessTask selectOne = processTaskDao.selectOne(new LambdaQueryWrapper<ProcessTask>()
                .eq(ProcessTask::getPracticalId, practicalId)
                .eq(ProcessTask::getProcessType, 1));


//      若为最终任务selectone，判断是否已发布
        long processType = processTask.getProcessType();
        if (processType == 1) {
//          已发布最终
            if (selectOne != null)
                return new R(Code.SAVE_ERR, null, "该任务已发布最终任务");
//          未发布 continue
        }


//      数据库新曾任务【过程/最终】
        int insert = processTaskDao.insert(processTask);

//      响应数据
        if (processType != 1) {

            return new R(Code.SAVE_OK, insert, "发布过程任务成功");
        } else {
            PracticalTask practicalTask = practicalTaskDao.selectOne(new LambdaQueryWrapper<PracticalTask>()
                    .eq(PracticalTask::getPracticalId, practicalId));
            long classId = practicalTask.getClassId();
//      生成自动打分表
            scoreService.setMouses(classId, processTask.getProcessId());

            return new R(Code.SAVE_OK, insert, "发布最终任务成功，已生成互评打分表");
        }
    }

    /**
     * 校内校外公用
     * 删除过程/最终任务
     *
     * @param processId
     * @param servletRequest
     * @return
     */
    @DeleteMapping("ProcessTask/{processId}")
    public R deleteProcessTask(@PathVariable long processId, ServletRequest servletRequest) {
        //获取我的电话号码
        String username = (String) servletRequest.getAttribute("username");

        int delete = processTaskDao.delete(new LambdaQueryWrapper<ProcessTask>()
                .eq(ProcessTask::getProcessId, processId));

        return new R(Code.DELETE_OK, delete, "删除成功");
    }


    @GetMapping("myInnerProcessTask")
    public R getmyInnerProcessTask(ServletRequest servletRequest) {
        //获取我的电话号码
        String username = (String) servletRequest.getAttribute("username");
        List<ProcessTaskVo> processTaskVos = processTaskVoDao.selectMyInnerProcessTaskByTeacherPhone(username);
        return new R(Code.SAVE_OK, processTaskVos, "查看我的inner过程任务");
    }

    @DeleteMapping("InnerPublish/{publishId}")
    public R deleteInnerPublishByPublishId(@PathVariable long publishId, ServletRequest servletRequest) {
//        获取我的电话号码
//        String username = (String) servletRequest.getAttribute("username");

        int delete = campusInnerPublishDao.delete(new LambdaQueryWrapper<CampusInnerPublish>().eq(CampusInnerPublish::getPublishId, publishId));
        if (delete > 0)
            return new R(Code.DELETE_OK, delete, "删除校内课题实践成功");
        else
            return new R(Code.DELETE_ERR, delete, "删除失败");

    }

    /**
     * 查看班级学生提交报告情况
     *
     * @param processId
     * @param servletRequest
     * @return
     */
    @GetMapping("processTask/{processId}")
    public R getProcessTask(@PathVariable long processId, ServletRequest servletRequest) {
        //获取我的电话号码
        String username = (String) servletRequest.getAttribute("username");
        List<ProcessTaskAo> processTaskAos = processTaskVoDao.selectProcessByProcessId(processId);

        return new R(Code.GET_OK, processTaskAos, "查看Inner/Course的ProcessTask本班提交情况");
    }


    /**
     * 修改密码
     *
     * @param servletRequest
     * @return
     */
    @PutMapping("updatePassword")
    public R updatePassword(@RequestBody Password password, ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");

        Teacher myDetil = teacherDao.selectOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeacherPhone, username));
        if (password.getOldPass().equals(myDetil.getTeacherPassword())) {
            int update = teacherDao.update(null, new LambdaUpdateWrapper<Teacher>()
                    .eq(Teacher::getTeacherPhone, username)
                    .set(Teacher::getTeacherPassword, password.getNewPass()));

            return new R(Code.UPDATE_OK, null, "修改密码成功！");
        } else

            return new R(Code.UPDATE_ERR, null, "旧密码输入错误！");
    }


    @GetMapping("/andCourse")
    public R getInnerAndCourseFinallProcess(ServletRequest servletRequest) {
        //获取我的电话号码
        String username = (String) servletRequest.getAttribute("username");
        List<PracticalTaskVo> practicalTaskVos = practicalTaskVoDao.selectVoForInnerAndCourseByAdviserPhone(username);
        return new R(Code.GET_OK, practicalTaskVos, "查看Inner/Course的ProcessTask本班提交情况");
    }


}
